//
//  ShellLiquidationViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/27.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellLiquidationViewModel.h"
#import "ShellChangeTeadePasswordViewController.h"


@interface ShellLiquidationViewModel ()

@property (nonatomic,assign) NSInteger pageIndex;

@end

@implementation ShellLiquidationViewModel

/**
 Convert  清算列表
 @param  isFirst 是否是第一次
 */
- (void)getLiquidationList:(BOOL)isFirst{
    
    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kPledgeDetail,self.pageIndex];
    @weakify(self);
    [LZNetworkingManager lz_request:@"get" url:url params:@{@"token":k_userToken?:@""} success:^(id  _Nullable responseObject) {
        @strongify(self);
        NSArray *dataArray = responseObject[kResultData];
        if(isFirst){
            [self.dataArray removeAllObjects];
        }
        for (int i = 0; i < dataArray.count; i++) {
            ShellLiquidationModel *liquidationModel = [ShellLiquidationModel mj_objectWithKeyValues:dataArray[i]];
            [self.dataArray addObject:liquidationModel];
        }
       //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
       NSInteger totalCount = [responseObject[@"total"] integerValue];
       if(self.dataArray.count == totalCount && self.dataArray.count > 0){
           //没有更多数据
           [self.reloadSubject sendNext:@(0)];
           return;
       }
       //看是否有数据
       if(self.dataArray.count == 0){
           [self.reloadSubject sendNext:@(0)];
       } else {
           self.pageIndex++;
           [self.reloadSubject sendNext:@(1)];
       }
       
   } failure:^(NSError * _Nullable error) {
       
       [self.reloadSubject sendNext:@(0)];
       
   } notwork:^(NSError * _Nullable error) {
       [self.reloadSubject sendNext:@(0)];
       
   } isLoading:NO isFailTip:YES];

    
}

/**
 Convert 获取清算文案
 */
- (void)liquidationDesc{
    
    @weakify(self);
    ///清算的文案
    self.liquidationModel = [[ShellLiquidationModel alloc] init];
    [LZNetworkingManager lz_request:@"get" url:kClearTextDesc params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
          @strongify(self);
          self.liquidationModel = [ShellLiquidationModel mj_objectWithKeyValues:responseObject[kResultData]];
        }
        [self.reloadSubject sendNext:@(1)];
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
}

/**
 Convert 清算数据
 */

- (void)liquidation:(ShellLiquidationModel *)liquidationModel{
 
    @weakify(self);
    [LZToolView showAlertType:LZAlertViewTypeTipAlter withTitle:LZLocalizedString(@"提示") message:self.liquidationModel.confirmText?:@"" cancel:LZLocalizedString(@"取消") sure:LZLocalizedString(@"确定") objectDict:@{@"textAlignment":@(NSTextAlignmentCenter)} cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
        NSDictionary *params = @{@"detailId":liquidationModel.modelId?:@""};
        
        [LZNetworkingManager lz_request:@"post" url:kClear params:params success:^(id  _Nullable responseObject) {
          if([responseObject[kResultStatus] isEqualToString:kCode]){
            @strongify(self);
            self.liquidationModel.textDec = responseObject[kResultData];
              [LZToolView dismiss];
               //清算成功
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                   [LZToolView showAlertType:LZAlertViewTypeLiquidation withTitle:LZLocalizedString(@"清算成功") message:LZLocalizedString(@"清算成功的文案") cancel:@"" sure:LZLocalizedString(@"确定") objectDict:@{} cancelBlock:^(id  _Nullable object) { } sureBlock:^(id  _Nullable object) {}];
               });
              
              [self getLiquidationList:YES];
              
          }
       } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
        
        
    }];
    
    

}
- (void)liquidationPassword:(NSString *)password model:(ShellLiquidationModel *)liquidationModel{
    
    
}
@end
